//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet
{
    public class ExpressionFormattingRule : StandardConditionalFormattingRule
    {
        public ExpressionFormattingRule()
        {
            
        }
        internal override ConditionalFormattingType Type
        {
            get { return ConditionalFormattingType.Expression; }
        }

#if DEBUG
        private static bool messageWritten;


#endif

        internal override bool Apply(Cell cell)
        {
            var expressionFormula = FormulaObject1 as ExpressionFormula;
            if (expressionFormula == null)
                return false;
            Cell topLeft = Parent.Range.TopLeftCell;
            Expression expression = expressionFormula.Expression.Offset(cell.Address - topLeft.Address);
#if DEBUG
            string previousExpressionText =
                expressionFormula.Expression.GetText(new TextContext(new CellEvaluationContext(cell.Worksheet[topLeft.Address])));
            string expressionText = expression.GetText(new TextContext(new CellEvaluationContext(cell)));
            Debug.WriteLine(expressionText);
#endif
            object value = expression.GetValue(new CellEvaluationContext(cell));
            if (value is bool)
            {
                return ApplyStyle(cell, (bool) value);
            }
            //\\throw new System.NotImplementedException();)
            return false;
        }

        internal override ConditionalFormattingRule Clone()
        {
            throw new NotImplementedException();
        }
    }
}